package com.lanxi.nbpaytest;

import com.alibaba.fastjson.JSON;

import java.util.Map;

/**
 * @Author mintaoyu
 * Date on 2020-03-03  16:07
 */
public class RSAUtil {

    public static boolean RSA(String dataContent, String signContent) throws Exception {
        // 手机公钥 (可直接使用)
        String publicKeyNb = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCRWTUTW0JJamT0Ii5V9/cD5vWM/0kGqZpI9LWQvesNFge6MNE+sRtdD3E+pJX6rBNWGNOoGPi3dZJcXETrZtOXskuqdkWyM+/a1RwPxMF9ip8B0+xkjStb3LlpJpo/RSIQlQvHc+QRJHnC4qhIy7Yjm7BWMOAYgB4BBuDBbiyNxwIDAQAB";
        // 蓝喜私钥 (建议重新生成一对公私钥，然后将私钥替换本字符串值，公钥发给手机银行)
        String privateKeyMy = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIdXdI7bwzMEUgUf7YFgV019V1sObN3PqdT8DQiyOMdIOrxqisflHuRePnGNi5rW58dsvvegBEVvmaLLk1qaercA1XfGgzdLPT1ZrN4CFBYXVTDECguT3R4pz9lRJrs6KR3XYuc7FqeV/wDuEgQFV7LgTjZ6p7Gaa65wwhv218ODAgMBAAECgYBirflmUAW7JONIqUbalgwRi9otog7ui4Mgr8YxGQEdDCUHTAd0SDKF/9E3qiSn+Rl15mw20I1Ms1opwJTn8sqVpJsw3oBO18noGN00xi9ybIAKCBdLW9aQVhX+i1s6l3n8awM96774mii/PCUyXRyAEgEKA1awpyw3TqUN8SAiAQJBAL6M4gzyIjflKmAvdRrWSYASHBI4OQXvWAbluakUftjuvJzinyt74qDvMHebFbAXs0xylFwHPKo+eV/LVSk4p6ECQQC11BA1VrjMaFGI3pXCURUsfFla2bumkA+HpLWSauxYt6MpncOLcUIO5hrBRglHf4gB6lAFKhmoqSpLDuHFiAijAkBXz2KIZrjAG1oo0xXbFS7v1uiauNAGXSKqowio0yoVXAcNIR4Kkt+AHFmwdIZNDM7yhsa5lhgtt/aE35XSFiqhAkB0la1y1dEV10l4LefuORkPdgfBFdyY9sPOYFC/uekjFVP76hkofo0xil7bT/HBM0dcmwWF2jGrkBP7HmaqxgdrAkEAjhLHDWYfEXPHmg25oE+oR8xbr6Sfr9QSbkSvWsCPv09LoDgV6bDt9Tswdyd5/Oz8PP/dR8EI2FNH/N3FmtX6VA==";
//        // 密文内容 (从url后拼接的参数获取，字段名: dataContent)
//        String dataContent = dataContent;
//        // 签名内容 (从url后拼接的参数获取，字段名: signContent)
//        String signContent = signContent;

        Boolean creatKey = false;    // true-生成秘钥 false-解密
        try {
            if (creatKey) {
                Map<String, String> map = NbankRSAUtil.generateKey(); // 生成秘钥
                String privateKeyStr = map.get("privateKey"); // 获取私钥
                String publicKeyStr = map.get("publicKey"); // 获取公钥
                System.out.println("生成私钥:\n" + privateKeyStr + "\n生成公钥:\n" + publicKeyStr);
            } else {
                // 验签
                Boolean dataCheck = NbankRSAUtil.verifySignByPublicKey(dataContent, publicKeyNb, signContent);
                if (dataCheck) {
                    System.out.println("验签结果: " + dataCheck);
                    String data2 = NbankRSAUtil.decrypt(dataContent, privateKeyMy);
                    System.out.println("解密内容: " + data2);
                    Map<String, String> map = (Map<String, String>) JSON.parse(data2);
                    String mobile = map.get("mobile");
                    System.out.println("手机号码: " + mobile);
                    return true;
                } else {
                    System.out.println("验签失败！");
                    return false;
                }
            }
        } catch (Exception e) {
            throw new Exception("验签失败!" + e);
        }
        return false;
    }


//    public static byte[] readInputStream(InputStream inStream) throws Exception {
//        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
//        byte[] buffer = new byte[1024];
//        boolean var3 = false;
//
//        int len;
//        while ((len = inStream.read(buffer)) != -1) {
//            outSteam.write(buffer, 0, len);
//        }
//        outSteam.close();
//        inStream.close();
//        return outSteam.toByteArray();
//    }


}
